package com.itheima.set;

import com.itheima.domain.Student;


import java.util.Comparator;
import java.util.TreeSet;

/**
 * 比较器设置 TreeSet排序规则
 */
public class Test04 {

    public static void main(String[] args) {
        //第一种   创建集合是指定 排序规则
        TreeSet<Teacher> set= new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //o1 要存的元素和  o2 已经在 集合中的
                return o1.getAge()-o2.getAge()==0?o1.getName().compareTo(o2.getName()):o1.getAge()-o2.getAge();
            }

        });

        set.add(new Teacher("aaaa",28));
        set.add(new Teacher("aaaa",28));
        set.add(new Teacher("bbbb",29));
        set.add(new Teacher("bbbb",28));
        set.add(new Teacher("cccc",30));
        set.add(new Teacher("dddd",25));
        set.add(new Teacher("eeeee",23));
        System.out.println(set);
        //第二种
       Comparator<Teacher> comparator= (o1,o2)->o1.getAge()-o2.getAge()==0?o1.getName().compareTo(o2.getName()):o1.getAge()-o2.getAge();

        TreeSet<Teacher> set2= new TreeSet<>(comparator);
        set2.add(new Teacher("aaaa",28));
        set2.add(new Teacher("aaaa",28));
        set2.add(new Teacher("bbbb",29));
        set2.add(new Teacher("bbbb",28));
        set2.add(new Teacher("cccc",30));
        set2.add(new Teacher("dddd",25));
        set2.add(new Teacher("eeeee",23));
        System.out.println(set2);

    }
}
class Teacher{

    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}